<?php

require_once('./discuz_publish.php');

if($_GET['ii']) $ii = $_GET['ii'];
else $ii = 30;

if($_GET['k']) $kk = $_GET['k'];
else $kk = 0;

for($i=$ii; $i>=0; $i--)
{
	$base_url = "http://programme.77169.com/List/List_56" . ($i==0 ? '' : '_' . $i) . ".html";
	$content = file_get_contents($base_url);
	file_put_contents( dirname(__FILE__ ) . '/log', $title . '{br}'. $content . '{br}');

	preg_match_all('/\]<a class="listA" href="(.*?)" title="[^~]*?" target="_blank">(.*?)<\/a>/i', $content, $matches);

	if(is_array($matches))
	{
		$hrefs = $matches[1];
		$titles = $matches[2];

		foreach($hrefs as $k=>$v)
		{
			if($k >= $kk)
			{
				$title = $titles[$k];
				$message = get_message($v);

				discuz_publish(escape_s($title), escape_s($cont), 77);
				
				if ($d++ > 10)
				{
					header("Location: ./" . __FILE__ . '?ii=' . $i . '&k=' . $d); 
					exit(0);
				}
			}
		}
		
		header("Location: ./" . __FILE__ . '?ii=' . ($i--) ); 
		exit(0);
	}
}

function get_message($v)
{
	$data = file_get_contents($v);
	$data = str_replace('~', '', $data);
	preg_match_all('/<td class=main_tdbg_760 [^~]*?<\/table>([^~]*?)<\/td>/', $data, $matches);

	$data = trim($matches[1][0]);
	$data = data_clean($data);
	return $message;
}

function data_clean($data)
{
	$data = preg_replace('/<BR>/i', "\n", $data);
	$data = preg_replace('/<p>/i', "\n", $data);
	$data = preg_replace('/<(.*?)>/i', '', $data);
	$data = str_replace('&nbsp;', ' ', $data);
	return $data;
}



?>